// pages/list/list.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    no_pic:('/static/images/no-pic.png'),
    id:'',
    searchKey: '', //搜索关键词
    width: 200, //header宽度
    height: 64, //header高度
    inputTop: 0, //搜索框距离顶部距离
    arrowTop: 0, //箭头距离顶部距离
    dropScreenH: 0, //下拉筛选框距顶部距离
    dropScreenShow: false,
    scrollTop: 0,
    tabIndex: 0, //顶部筛选索引
    isList: false, //是否以列表展示  | 列表或大图
    selectedName: '综合',
    selectH: 0,
    dropdownList: [{
        name: '综合',
        selected: true
      },
      {
        name: '价格升序',
        selected: false
      },
      {
        name: '价格降序',
        selected: false
      }
    ],
    pageIndex: 1,
    productList:[],
    loadding: false,
    allDone:false,
    pullUpOn: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that=this;
    let obj = {};
    obj = wx.getMenuButtonBoundingClientRect();
    wx.getSystemInfo({
      success: res => {
        let width = obj.left || res.windowWidth;
        let height = obj.top ? obj.top + obj.height + 8 : res.statusBarHeight + 44;
        let inputTop = obj.top ? obj.top + (obj.height - 30) / 2 : res.statusBarHeight + 7;
        let arrowTop = obj.top ? obj.top + (obj.height - 32) / 2 : res.statusBarHeight + 6;
        let searchKey = options.searchKey || '';
        let id = options.id || '';
        //略小，避免误差带来的影响
        let dropScreenH = (height * 750) / res.windowWidth;
        let drawerH = res.windowHeight - 100/2 - height;
        that.setData({
          width: width,
          height: height,
          inputTop: inputTop,
          arrowTop: arrowTop,
          searchKey: searchKey,
          id:id,
          dropScreenH: dropScreenH,
          drawerH:drawerH
        })
        that.load();
      }
    });
  },

  async load(){
    if(this.data.allDone){
      return;
    }
    if(this.data.loadding){
      return;
    }
    let sub={};
    sub.id=this.data.id;
    sub.key=this.data.searchKey;
    sub.page=this.data.pageIndex;
    sub.tabIndex=this.data.tabIndex;
    sub.sort="";
    if(sub.tabIndex==0){
      this.data.dropdownList.forEach((v,i)=>{
        if(v.selected){
          sub.sort=v.name;
        }
      })
    }
    let res=await wx.util.post("cate/itemList",sub,this.data.pageIndex>1);
    if(res.data.data.length>0){
      this.setData({
        pageIndex:this.data.pageIndex++
      })
    }
    if(res.data.data.length==0 || res.data.data.length<res.data.per_page){
      this.setData({
        allDone:true
      })
    }
    this.setData({
      productList:this.data.productList.concat(res.data.data),
      loadding:false,
      pullUpOn:false
    })
  },
  reload(){
    this.setData({
      allDone:false,
      pullUpOn:false,
      loadding:false,
      pageIndex:1,
      productList:[]
    })
    this.load();
  },
  px(num) {
    return num + 'px';
  },
  btnCloseDrop() {
    this.setData({
      scrollTop:0,
      dropScreenShow:false,
      attrIndex:1
    })
  },
  showDropdownList: function() {
    this.setData({
      selectH:246,
      tabIndex:0
    })
  },
  hideDropdownList: function() {
    this.setData({
      selectH:0
    })
  },
  dropdownItem: function(e) {
    let index = Number(e.currentTarget.dataset.index);
    let arr = this.data.dropdownList;
    for (let i = 0; i < arr.length; i++) {
      if (i === index) {
        arr[i].selected = true;
      } else {
        arr[i].selected = false;
      }
    }
    this.setData({
      dropdownList:arr,
      selectH:0,
      selectedName:index == 0 ? '综合' : '价格'
    })
    this.reload();
  },
  screen: function(e) {
    let index = e.currentTarget.dataset.index;
    this.hideDropdownList();
    this.btnCloseDrop();
    if (index == 0) {
      this.showDropdownList();
      return;
    } else if (index == 1) {
      this.setData({
        tabIndex:1,
      })
    } else if (index == 2) {
      this.setData({
        isList:!this.data.isList
      })
      return;
    } else if (index == 3) {
      this.setData({
        drawer:true
      })
    }
    this.reload();
  },
  back: function() {
    wx.navigateBack();
  },
  search: function() {
    wx.redirectTo({
      url: '/pages/search/search'
    })
  },
  onReachBottom: function() {
    this.load();
  }
})